package dtd.PHS.VND_ExchangeRate.data;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

import dtd.PHS.VND_ExchangeRate.data.RateItem.MyCurrency;


public class VCBRatesParser {
	
	static public final String VCB_URL = "http://www.vietcombank.com.vn/en/exchange%20rate.asp";
	static private final String PREVIOUS_LINE = "<tr VALIGN=\"TOP\">";
	static private final String INPUT_DATE_TITLE = "<b>Input date</b>";

	
	private MyDate date;
	private ListRates listRates;

	public VCBRatesParser() {	
		listRates = new ListRates();
		date = null;
	}

	public void parse() throws IOException {
		
		URL vcbURL = new URL(VCB_URL);
		BufferedReader in = new BufferedReader(new InputStreamReader(vcbURL.openStream()));
		String inputLine="ABC";
		String previousLine;
		while (inputLine != null) {
			previousLine = inputLine;
			inputLine = in.readLine();
			if ( this.containInputDate(previousLine)) {
				this.extractInputDate(inputLine);
			}
			for (MyCurrency c : MyCurrency.values()) {
				if (this.containsInfoOf(c,inputLine,previousLine)) {
					RateItem excRate = this.extractCurrencyInfo(c,previousLine,inputLine,in);
					this.listRates.add(excRate);
				}
			}

		}
		in.close();
	}
	
	public RateItem extractCurrencyInfo(
			MyCurrency c, String previousLine,
			String inputLine, BufferedReader in) {
		try {
			//2 skip lines
			String skipLine = in.readLine();
			String fullCurrencyName = in.readLine();

			double buying = this.extractRateFromLine(in);			
			double transfer = this.extractRateFromLine(in);
			double selling = this.extractRateFromLine(in);

			return new RateItem(c,buying, transfer, selling);
		} catch (Exception e) {
			System.out.println(e.getMessage());			
			return null;
		}

	}
	
	private double extractRateFromLine(BufferedReader in) {
		try {
			String skipLine = in.readLine();
			String fullLine = in.readLine();
			int indexBr = fullLine.indexOf("<br>");
			int numberPos = fullLine.lastIndexOf(">", indexBr) + 1;
			String numberString = fullLine.substring(numberPos, indexBr);
			numberString = numberString.replaceAll(",", "");
			return Double.parseDouble(numberString);
		} catch (Exception e) {
			System.out.println(e.getMessage());			
		}
		return 0;
	}
	
	
	public boolean containsInfoOf(MyCurrency c, String inputLine, String previousLine) {
		if ( ! (previousLine.trim().equals(PREVIOUS_LINE)) ) {
			return false;
		}
		if (! inputLine.contains(c.toString())) {
			return false;
		}		
		return true;
	}
	
	private boolean containInputDate(String previousLine) {
		return previousLine.contains(INPUT_DATE_TITLE);		
	}

	private void extractInputDate(String inputLine) throws NumberFormatException {		
		String inputDate = inputLine.substring(
				inputLine.indexOf("value")+7,
				inputLine.indexOf("maxlength")-2);
		try {
			date = new MyDate( inputDate );
		} catch (NumberFormatException e) {			
			throw e;
		}
		
	}

	public ListRates getRates() {
		return listRates;
	}

	public MyDate getDate() {
		return date;
	}

}
